package com.app.web.controller;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.app.biz.AppCategoryBiz;
import com.app.biz.AppInfoBiz;
import com.app.biz.AppVersionBiz;
import com.app.entity.AppCategory;
import com.app.entity.AppInfo;
import com.app.entity.AppVersion;

@Controller
public class AddEditController {
	@Autowired
	private AppCategoryBiz appCategoryBiz;
	@Autowired
	private AppVersionBiz appVersionBiz;
	@Autowired
	private AppInfoBiz appInfoBiz;

	@RequestMapping("addapp")
	public String addapp(Model model) {
		// int[] array2={1,2};查询父类是？的二级分类
		// List<AppCategory>
		// categoryLevel2=appCategoryBiz.getAppCategoryByParendId(categoryLevel1);
		// model.addAttribute("categoryLevel2",categoryLevel2);

		// int[] array3={3,4,19,20,21,22};
		// List<AppCategory>
		// categoryLevel3=appCategoryBiz.getAppCategoryByParendId(2);
		// model.addAttribute("categoryLevel3",categoryLevel3);
		return "addapp";
	}

	@RequestMapping(value = "/categoryLevel2", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAllcategoryLevel2(Integer categoryLevel1) {
		System.out.println(categoryLevel1);
		List<AppCategory> list = appCategoryBiz
				.getAppCategoryByParendId(categoryLevel1);
		String result = JSON.toJSONString(list, true);
		return result;
	}

	@RequestMapping(value = "/categoryLevel3", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAllcategoryLevel3(Integer categoryLevel2) {
		System.out.println(categoryLevel2);
		List<AppCategory> list = appCategoryBiz
				.getAppCategoryByParendId(categoryLevel2);
		String result = JSON.toJSONString(list, true);
		return result;
	}

	@RequestMapping("/addInfoById")
	public String addInfoById(Model model, Integer id) {
		List<AppVersion> appVersionList = appVersionBiz.getAllAppVersions(id);
		model.addAttribute("appVersionList", appVersionList);
		return "addInfoById";
	}

	// 添加appInfo信息
	@RequestMapping(value = "/appInfo", method = RequestMethod.POST)
	public String appInfo(Model model, MultipartFile logoPicPath,
			HttpServletRequest request, AppInfo appInfo, BindingResult result) {
		// 获取MovieImages的物理路径
		if (!logoPicPath.isEmpty()) {
			String path = request.getServletContext().getRealPath("statics/uploadfiles");
			String filename = path + "/" + logoPicPath.getOriginalFilename();
			System.out.println(filename);
			//System.out.println("////"+appInfo.getLogoPicPath());
			try {
				FileOutputStream fileOutputStream = new FileOutputStream(
						filename);
				fileOutputStream.write(logoPicPath.getBytes());
				fileOutputStream.close();
				System.out.println("上传成功！！");
				appInfo.setDevId(1);
				appInfo.setLogoPicPath("statics/uploadfiles/"
						+logoPicPath.getOriginalFilename());
				appInfo.setLogoLocPath("D:\\soft\\apache-tomcat-7.0.41\\webapps\\AppInfoSystem\\statics\\uploadfiles\\"
						+ logoPicPath.getOriginalFilename());
				int re = appInfoBiz.addInfo(appInfo);
				if (re > 0) {
					System.out.println("掐入成功");
				} else {
					System.out.println("掐入失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "redirect:information";
	}
	// 添加appVersion信息
		@RequestMapping(value = "/addVersion", method = RequestMethod.POST)
		public String appVersion(Model model, MultipartFile apkFileName,
				HttpServletRequest request,AppVersion appVersion, BindingResult result) {
			// 获取MovieImages的物理路径
			if (!apkFileName.isEmpty()) {
     			String apkFileName1=apkFileName.getOriginalFilename();
				System.out.println("原来："+apkFileName1);
				//System.out.println(appVersion.getAppInfo());
//				int bity=apkFileName1.length();
//				int index=apkFileName1.lastIndexOf("\\");
//				System.out.println(bity+"/"+index);
//				String appFile=apkFileName1.substring(index, bity);
//				System.out.println(appFile);
				String path = request.getServletContext().getRealPath("statics\\uploadfiles");
				String filename = path + "\\"+apkFileName1;
				System.out.println(filename);
				try {
					FileOutputStream fileOutputStream = new FileOutputStream(
							filename);
					fileOutputStream.write(apkFileName.getBytes());
					fileOutputStream.close();
					System.out.println("上传成功！！");	
					appVersion.setPublishStatus(3);
					appVersion.setDownloadLink("statics/uploadfiles/"+apkFileName1);
					appVersion.setCreatedBy(1);
					Date date =new Date();
					appVersion.setCreationDate(date);
					appVersion.setApkLocPath("D:\\soft\\apache-tomcat-7.0.41\\webapps\\AppInfoSystem\\statics\\uploadfiles\\"+apkFileName1);
					appVersion.setApkFileName(apkFileName1);
					int re = appVersionBiz.addVersion(appVersion);
					if (re > 0) {
						System.out.println("掐入成功");
					} else {
						System.out.println("掐入失败");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return "redirect:information";
		}

}
